package com.atg.atgchat.init.user.service.imp;

import com.atg.atgchat.init.common.event.UserRegisteredEvent;
import com.atg.atgchat.init.common.utils.AssertUtil;
import com.atg.atgchat.init.user.dao.ItemConfigDao;
import com.atg.atgchat.init.user.dao.UserBackpackDao;
import com.atg.atgchat.init.user.dao.UserDao;
import com.atg.atgchat.init.user.model.dto.WearingBadgeRequest;
import com.atg.atgchat.init.user.model.entity.ItemConfig;
import com.atg.atgchat.init.user.model.entity.User;
import com.atg.atgchat.init.user.mapper.UserMapper;
import com.atg.atgchat.init.user.model.entity.UserBackpack;
import com.atg.atgchat.init.user.model.enums.ItemEnum;
import com.atg.atgchat.init.user.model.enums.ItemTypeEnum;
import com.atg.atgchat.init.user.model.vo.BadgeVo;
import com.atg.atgchat.init.user.model.vo.UserInfoVo;
import com.atg.atgchat.init.user.model.vo.adapter.UserAdapter;
import com.atg.atgchat.init.user.service.UserService;
import com.atg.atgchat.init.user.service.cache.ItemCache;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户表 服务实现类
 *
 * @author author: atg
 * @since 2025-01-12
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private UserBackpackDao userBackpackDao;
    @Resource
    private ItemCache itemCache;
    @Resource
    private ItemConfigDao itemConfigDao;

    // 事件---用于发布应用程序事件
    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public Long register(User buildUser) {
        userDao.save(buildUser);
        // 注册事件--注册之后送东西
        applicationEventPublisher.publishEvent(new UserRegisteredEvent(this, buildUser));
        return buildUser.getId();

    }

    @Override
    public UserInfoVo getUserInfo(Long uid) {
        User byId = userDao.getById(uid);
        // 获取改名卡的次数
        Integer countModifyName = userBackpackDao.getCountModifyName(uid, ItemEnum.MODIFY_NAME_CARD.getId());
        return UserAdapter.buildUserInfoVo(byId, countModifyName);
    }

    @Override
    @Transactional
    public void modifyName(Long uid, String name) {
        // 修改名字--看重复不重复
        User userName = userDao.getUserName(name);
        AssertUtil.isEmpty(userName, "名字重复,请换一个哦~");
        // 看改名卡够不够用
        UserBackpack firstValid = userBackpackDao.getFirstValid(uid, ItemEnum.MODIFY_NAME_CARD.getId());
        AssertUtil.isNotEmpty(firstValid, "改名卡不足,请等待时机哦~");
        // 使用改名卡
        boolean status = userBackpackDao.updateStatus(firstValid.getId());
        AssertUtil.isTrue(status, "改名卡使用失败,请重试哦~");
        userDao.modifyName(uid, name);
    }

    @Override
    public List<BadgeVo> badges(Long uid) {
        // 显示所有的徽章
        List<ItemConfig> itemConfigs = itemCache.getByType(ItemTypeEnum.BADGE.getType());
        // 查询当前用户的徽章                         提取用户具有的ID
        List<UserBackpack> daoByItemIds = userBackpackDao.getByItemIds(uid, itemConfigs.stream().map(ItemConfig::getId).collect(Collectors.toList()));
        // 查询当前用户具有的徽章
        User user = userDao.getById(uid);
        return UserAdapter.buildBadgeVo(itemConfigs, daoByItemIds, user);
    }

    @Override
    public void wearBadge(Long uid, WearingBadgeRequest wearingBadgeRequest) {
        // 获取拥有的徽章
        UserBackpack firstValid = userBackpackDao.getFirstValid(uid, wearingBadgeRequest.getBadgeId());
        AssertUtil.isNotEmpty(firstValid, "您没有徽章,请等待时机哦~");
        // 确保物品是徽章
        ItemConfig byId = itemConfigDao.getById(wearingBadgeRequest.getBadgeId());
        AssertUtil.isTrue(byId.getType().equals(ItemTypeEnum.BADGE.getType()), "物品不是徽章,请换一个哦~");
        // 佩戴徽章
        userDao.wearingBadge(uid, wearingBadgeRequest.getBadgeId());
        //删除用户缓存


    }
}
